using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Models;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Models.Openmerch.Query
{
    /// <summary>
    /// 商户费率查询响应
    /// </summary>
    public class MerchFeeQueryResponse : StandardResponse, IComplexFieldHandler
    {
        /// <summary>
        /// 商户号
        /// </summary>
        [JsonPropertyName("merch_no")]
        public string? MerchNo { get; set; }

        /// <summary>
        /// 费率信息列表
        /// </summary>
        [JsonPropertyName("rate_info")]
        public List<RateInfo>? RateInfo { get; set; }

        /// <summary>
        /// 处理复杂字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        public void HandleComplexField(string fieldName, object fieldValue)
        {
            if (fieldName == "rate_info")
            {
                if (fieldValue is JsonElement jsonElement)
                {
                    if (jsonElement.ValueKind == JsonValueKind.Array)
                    {
                        RateInfo = new List<RateInfo>();
                        foreach (var item in jsonElement.EnumerateArray())
                        {
                            if (item.ValueKind == JsonValueKind.Object)
                            {
                                var rateInfo = JsonSerializer.Deserialize<RateInfo>(item.GetRawText(), new JsonSerializerOptions 
                                { 
                                    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower 
                                });
                                if (rateInfo != null)
                                {
                                    RateInfo.Add(rateInfo);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 费率信息
    /// </summary>
    public class RateInfo
    {
        /// <summary>
        /// 费率计算类型
        /// </summary>
        [JsonPropertyName("fee_calc_type")]
        public string? FeeCalcType { get; set; }

        /// <summary>
        /// 最小金额
        /// </summary>
        [JsonPropertyName("min_amt")]
        public string? MinAmt { get; set; }

        /// <summary>
        /// 最大金额
        /// </summary>
        [JsonPropertyName("max_amt")]
        public string? MaxAmt { get; set; }

        /// <summary>
        /// 是否固定费率
        /// </summary>
        [JsonPropertyName("fixed")]
        public string? Fixed { get; set; }

        /// <summary>
        /// 费率
        /// </summary>
        [JsonPropertyName("rate")]
        public string? Rate { get; set; }

        /// <summary>
        /// 最低费用
        /// </summary>
        [JsonPropertyName("bottom")]
        public string? Bottom { get; set; }

        /// <summary>
        /// 最高费用
        /// </summary>
        [JsonPropertyName("top")]
        public string? Top { get; set; }

        /// <summary>
        /// 阶梯
        /// </summary>
        [JsonPropertyName("step")]
        public string? Step { get; set; }

        /// <summary>
        /// 基础金额
        /// </summary>
        [JsonPropertyName("base_amount")]
        public string? BaseAmount { get; set; }

        /// <summary>
        /// 基础费用
        /// </summary>
        [JsonPropertyName("base_fee")]
        public string? BaseFee { get; set; }
    }
} 